package cn.lyh.FoodSystem.Service.ImpI;

import cn.lyh.FoodSystem.Dao.StudyDao;
import cn.lyh.FoodSystem.Dto.PageInfo;
import cn.lyh.FoodSystem.Dto.Result;
import cn.lyh.FoodSystem.Entity.Study;
import cn.lyh.FoodSystem.Service.StudyService;
import cn.lyh.edu.domain.EduMember;
import cn.lyh.edu.repository.EduMemberRepository;
import cn.lyh.system.domain.SysUser;
import cn.lyh.system.repository.SysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class StudyServiceImpI implements StudyService {

    @Autowired
    private StudyDao studyDao;

    @Autowired
    private EduMemberRepository userDao;




    /**
     * 添加学习记录
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result addStudy(Study study){
        Result result = new Result();
        try {

        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }
    /**
     * 查询学习记录
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result queryStudy(SysUser user){
        Result result = new Result();
        try {

        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }
    /**
     * 查询学习记录 后台
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result queryStudyAll(PageInfo pageInfo){
        Result result = new Result();
        try {
            Pageable pageable = PageRequest.of(pageInfo.getPage(), pageInfo.getLimit());
            Page<Study> studyPage = studyDao.findAll(pageable);
            List<Study> studyList =studyPage.getContent();
            for (Study item:studyPage.getContent()){
                Long id = Long.valueOf(item.getUserId());
                List<EduMember> userList=userDao.findAllById(Collections.singleton(id));
                item.setUser(userList.get(0));
            }
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(studyPage);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }
    /**
     * 更新学习记录
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result updateStudy(Study study){
        Result result = new Result();
        try {

        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }
    /**
     * 删除学习记录
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result deleteStudy(Study study){
        Result result = new Result();
        try {
         studyDao.deleteById(study.getId());
         result.setMsg("删除成功");
         result.setCode(200);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }
}
